load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
load("//bazel:fuzz_testing.bzl", "DEFAULT_RUSTC_FLAGS_FOR_FUZZING", "rust_fuzz_test_binary", "rust_fuzz_test_binary_afl")

package(default_visibility = [
    "__subpackages__",
    "//rs/execution_environment/fuzz:__subpackages__",
])

rust_library(
    name = "wasm_fuzzers",
    testonly = True,
    srcs = glob(["src/*.rs"]),
    crate_features = select({
        "//bazel:fuzzing_code_enabled": ["fuzzing_code"],
        "//conditions:default": [],
    }),
    rustc_flags = select({
        "//bazel:fuzzing_code_enabled": DEFAULT_RUSTC_FLAGS_FOR_FUZZING,
        "//conditions:default": [],
    }),
    version = "0.1.0",
    deps = [
        # Keep sorted.
        "//rs/config",
        "//rs/cycles_account_manager",
        "//rs/embedders",
        "//rs/interfaces",
        "//rs/monitoring/logger",
        "//rs/monitoring/metrics",
        "//rs/registry/subnet_type",
        "//rs/replicated_state",
        "//rs/test_utilities",
        "//rs/test_utilities/embedders",
        "//rs/test_utilities/state",
        "//rs/test_utilities/types",
        "//rs/types/management_canister_types",
        "//rs/types/types",
        "//rs/types/wasm_types",
        "@crate_index//:arbitrary",
        "@crate_index//:futures",
        "@crate_index//:lazy_static",
        "@crate_index//:libfuzzer-sys",
        "@crate_index//:num-traits",
        "@crate_index//:tokio",
        "@crate_index//:wasm-encoder",
        "@crate_index//:wasm-smith",
        "@crate_index//:wasmparser",
        "@crate_index//:wasmtime",
    ],
)

rust_test(
    name = "wasm_fuzzers_test",
    crate = ":wasm_fuzzers",
)

rust_binary(
    name = "ws2wasm",
    testonly = True,
    srcs = [
        "src/ws2wasm.rs",
    ],
    proc_macro_deps = [],
    version = "0.1.0",
    deps = [":wasm_fuzzers"] + [
        # Keep sorted.
        "//rs/config",
        "//rs/embedders",
        "//rs/monitoring/logger",
        "@crate_index//:arbitrary",
        "@crate_index//:clap",
        "@crate_index//:wasmprinter",
    ],
)

rust_fuzz_test_binary(
    name = "execute_with_wasmtime_libfuzzer",
    srcs = [
        "fuzz_targets/execute_with_wasmtime.rs",
    ],
    deps = [":wasm_fuzzers"] + ["@crate_index//:libfuzzer-sys"],
)

rust_fuzz_test_binary_afl(
    name = "execute_with_wasmtime_afl",
    srcs = [
        "fuzz_targets/execute_with_wasmtime.rs",
    ],
    deps = [":wasm_fuzzers"] + ["@crate_index//:libfuzzer-sys"],
)

rust_fuzz_test_binary_afl(
    name = "differential_simd_execute_with_wasmtime_afl",
    srcs = [
        "fuzz_targets/differential_simd_execute_with_wasmtime.rs",
    ],
    deps = [":wasm_fuzzers"] + ["@crate_index//:libfuzzer-sys"],
)

rust_fuzz_test_binary(
    name = "execute_with_wasm_executor_libfuzzer",
    srcs = [
        "fuzz_targets/execute_with_wasm_executor.rs",
    ],
    deps = [":wasm_fuzzers"] + ["@crate_index//:libfuzzer-sys"],
)

rust_fuzz_test_binary_afl(
    name = "execute_with_wasm_executor_afl",
    srcs = [
        "fuzz_targets/execute_with_wasm_executor.rs",
    ],
    deps = [":wasm_fuzzers"] + ["@crate_index//:libfuzzer-sys"],
)

rust_fuzz_test_binary_afl(
    name = "compile_wasm_using_embedder",
    srcs = ["fuzz_targets/compile_wasm_using_embedder.rs"],
    deps = [":wasm_fuzzers"] + ["@crate_index//:libfuzzer-sys"],
)
